package com.baihan.angus.app.game.busshow.ui.script_library.online_exhibition;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.library.baseAdapters.BR;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baihan.angus.app.game.basic.base.BaseActivity;
import com.baihan.angus.app.game.basic.configs.BasicConfig;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.databinding.ActivityExhibitionBinding;
import com.baihan.angus.app.game.busshow.ui.script_library.online_exhibition.appoint.AppointAreaFragment;
import com.baihan.angus.app.game.busshow.ui.script_library.online_exhibition.onlook.OnlookAreaFragment;
import com.baihan.angus.app.game.busshow.ui.script_library.online_exhibition.waiting.WaitingAreaFragment;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.google.android.material.tabs.TabLayout;
import com.gyf.immersionbar.ImmersionBar;

/**
 * 线上展会
 */
@Route(path = ArouterAppPath.EXHIBITION_ACTIVITY)
public class ExhibitionActivity extends BaseActivity<ActivityExhibitionBinding, ExhibitionVM> {

    private WaitingAreaFragment mWaitingAreaFragment;
    private AppointAreaFragment mAppointAreaFragment;
    private OnlookAreaFragment mOnlookAreaFragment;

    @Override
    protected void initStatusBar() {
        ImmersionBar.with(this).fitsSystemWindows(false).transparentStatusBar().statusBarDarkFont(BasicConfig.getInstance().isDayMode()).init();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_exhibition;
    }

    @Override
    protected int getViewModelId() {
        return BR.exhibitionVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        LinearLayout.LayoutParams rlTitle = (LinearLayout.LayoutParams) mVDBinding.title.getLayoutParams();
        rlTitle.topMargin = ImmersionBar.getStatusBarHeight(this);
        mVDBinding.title.setLayoutParams(rlTitle);

        mVDBinding.ivBack.setOnClickListener(v -> {
            if (mVDBinding.flSearch.getVisibility() == View.VISIBLE) {
                mVDBinding.flSearch.setVisibility(View.GONE);
                mVDBinding.flTab.setVisibility(View.VISIBLE);
            } else {
                finish();
            }
        });

        mVDBinding.ivSearch.setOnClickListener(v -> {
            mVDBinding.flSearch.setVisibility(View.VISIBLE);
            mVDBinding.flTab.setVisibility(View.GONE);
        });

        mVDBinding.flSearch.setOnClickListener(v -> {

        });

        mVDBinding.ivClearWord.setOnClickListener(v -> {
            mViewModel.ofEtWord.set("");
        });

        mWaitingAreaFragment = WaitingAreaFragment.newInstance();
        mAppointAreaFragment = AppointAreaFragment.newInstance();
        mOnlookAreaFragment = OnlookAreaFragment.newInstance();

        mVDBinding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                final TextView tabTv = (TextView) (((LinearLayout) ((LinearLayout) mVDBinding.tab.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));

                Animation animation = AnimationUtils.loadAnimation(ExhibitionActivity.this, R.anim.anim_tab_text_scale_big);
                animation.setFillAfter(true);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        tabTv.setTextAppearance(Typeface.BOLD);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                tabTv.setAnimation(animation);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                final TextView tabTv = (TextView) (((LinearLayout) ((LinearLayout) mVDBinding.tab.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));

                Animation animation = AnimationUtils.loadAnimation(ExhibitionActivity.this, R.anim.anim_tab_text_scale_small);
                animation.setFillAfter(true);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        tabTv.setTextAppearance(Typeface.NORMAL);
//                        tabTv.setTextColor(ContextCompat.getColor(getContext(), R.color.bg4));
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                tabTv.setAnimation(animation);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        mVDBinding.tab.addTab(mVDBinding.tab.newTab().setText(R.string.waitingArea), false);
        mVDBinding.tab.addTab(mVDBinding.tab.newTab().setText(R.string.appointArea), false);
        mVDBinding.tab.addTab(mVDBinding.tab.newTab().setText(R.string.onlookArea), false);

        initViewPager();
    }

    private void initViewPager() {
        mVDBinding.viewPager.setOffscreenPageLimit(2);
        mVDBinding.viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                if (position == 0) {
                    return mWaitingAreaFragment;
                } else if (position == 1) {
                    return mAppointAreaFragment;
                } else if (position == 2) {
                    return mOnlookAreaFragment;
                }
                return null;
            }

            @Override
            public int getCount() {
                return 3;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                if (position == 0) {
                    return getString(R.string.waitingArea);
                } else if (position == 1) {
                    return getString(R.string.appointArea);
                } else if (position == 2) {
                    return getString(R.string.onlookArea);
                }
                return mVDBinding.tab.getTabAt(position).getText();
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

            }
        });

        mVDBinding.tab.setupWithViewPager(mVDBinding.viewPager);
        mVDBinding.tab.getTabAt(0).select();
    }
}